using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Shared.Domain.Photos.AI
{
    /// <summary>
    /// AI照片分析实体
    /// 支持各种AI分析和识别功能
    /// </summary>
    public class AIPhotoAnalysis : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; protected set; }

        /// <summary>
        /// 照片ID
        /// </summary>
        public virtual Guid PhotoId { get; protected set; }

        /// <summary>
        /// 分析类型
        /// </summary>
        public virtual AnalysisType AnalysisType { get; protected set; }

        /// <summary>
        /// 分析状态
        /// </summary>
        public virtual AnalysisStatus Status { get; protected set; }

        /// <summary>
        /// 分析引擎
        /// </summary>
        [Required]
        [MaxLength(100)]
        public virtual string Engine { get; protected set; }

        /// <summary>
        /// 引擎版本
        /// </summary>
        [MaxLength(50)]
        public virtual string EngineVersion { get; protected set; }

        /// <summary>
        /// 分析模型
        /// </summary>
        [MaxLength(200)]
        public virtual string Model { get; protected set; }

        /// <summary>
        /// 模型版本
        /// </summary>
        [MaxLength(50)]
        public virtual string ModelVersion { get; protected set; }

        /// <summary>
        /// 分析参数
        /// </summary>
        public virtual Dictionary<string, object> Parameters { get; protected set; }

        /// <summary>
        /// 分析结果
        /// </summary>
        public virtual AIAnalysisResult Result { get; protected set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public virtual double Confidence { get; protected set; }

        /// <summary>
        /// 分析时间
        /// </summary>
        public virtual DateTime AnalysisTime { get; protected set; }

        /// <summary>
        /// 执行耗时（毫秒）
        /// </summary>
        public virtual long ExecutionDuration { get; protected set; }

        /// <summary>
        /// 处理优先级
        /// </summary>
        public virtual AnalysisPriority Priority { get; protected set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public virtual int RetryCount { get; protected set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        [MaxLength(2000)]
        public virtual string ErrorMessage { get; protected set; }

        /// <summary>
        /// 是否需要人工审核
        /// </summary>
        public virtual bool RequiresHumanReview { get; protected set; }

        /// <summary>
        /// 人工审核结果
        /// </summary>
        public virtual HumanReviewResult ReviewResult { get; protected set; }

        /// <summary>
        /// 分析配置
        /// </summary>
        public virtual AnalysisConfiguration Configuration { get; protected set; }

        /// <summary>
        /// 性能指标
        /// </summary>
        public virtual AIPerformanceMetrics PerformanceMetrics { get; protected set; }

        /// <summary>
        /// 照片导航属性
        /// </summary>
        public virtual Photo Photo { get; protected set; }

        protected AIPhotoAnalysis()
        {
            Parameters = new Dictionary<string, object>();
            Result = new AIAnalysisResult();
            ReviewResult = new HumanReviewResult();
            Configuration = new AnalysisConfiguration();
            PerformanceMetrics = new AIPerformanceMetrics();
        }

        public AIPhotoAnalysis(
            Guid id,
            Guid photoId,
            AnalysisType analysisType,
            string engine = "Default",
            string model = null,
            Dictionary<string, object> parameters = null,
            Guid? tenantId = null) : base(id)
        {
            PhotoId = photoId;
            AnalysisType = analysisType;
            Engine = engine;
            Model = model;
            Parameters = parameters ?? new Dictionary<string, object>();
            TenantId = tenantId;
            Status = AnalysisStatus.Pending;
            Priority = AnalysisPriority.Normal;
            AnalysisTime = DateTime.Now;

            Result = new AIAnalysisResult();
            ReviewResult = new HumanReviewResult();
            Configuration = new AnalysisConfiguration();
            PerformanceMetrics = new AIPerformanceMetrics();
        }

        /// <summary>
        /// 开始分析
        /// </summary>
        public virtual void StartAnalysis()
        {
            if (Status != AnalysisStatus.Pending && Status != AnalysisStatus.Retrying)
                throw new InvalidOperationException($"无法在状态 {Status} 下开始分析");

            Status = AnalysisStatus.Running;
            AnalysisTime = DateTime.Now;
        }

        /// <summary>
        /// 完成分析
        /// </summary>
        public virtual void CompleteAnalysis(AIAnalysisResult result, double confidence, long executionDuration)
        {
            Status = AnalysisStatus.Completed;
            Result = result ?? new AIAnalysisResult();
            Confidence = Math.Max(0, Math.Min(1, confidence));
            ExecutionDuration = executionDuration;

            // 更新性能指标
            PerformanceMetrics.ExecutionDuration = executionDuration;
            PerformanceMetrics.UpdateMetrics(this);

            // 检查是否需要人工审核
            RequiresHumanReview = ShouldRequireHumanReview();
        }

        /// <summary>
        /// 分析失败
        /// </summary>
        public virtual void FailAnalysis(string errorMessage)
        {
            Status = AnalysisStatus.Failed;
            ErrorMessage = errorMessage;
        }

        /// <summary>
        /// 取消分析
        /// </summary>
        public virtual void CancelAnalysis()
        {
            Status = AnalysisStatus.Cancelled;
        }

        /// <summary>
        /// 重试分析
        /// </summary>
        public virtual void RetryAnalysis()
        {
            if (RetryCount >= Configuration.MaxRetryCount)
            {
                FailAnalysis("已达到最大重试次数");
                return;
            }

            RetryCount++;
            Status = AnalysisStatus.Retrying;
            ErrorMessage = null;
        }

        /// <summary>
        /// 更新分析结果
        /// </summary>
        public virtual void UpdateResult(AIAnalysisResult result)
        {
            Result = result ?? new AIAnalysisResult();
        }

        /// <summary>
        /// 设置置信度
        /// </summary>
        public virtual void SetConfidence(double confidence)
        {
            Confidence = Math.Max(0, Math.Min(1, confidence));
        }

        /// <summary>
        /// 更新引擎信息
        /// </summary>
        public virtual void UpdateEngineInfo(string engine, string version, string model = null, string modelVersion = null)
        {
            Engine = engine;
            EngineVersion = version;
            Model = model;
            ModelVersion = modelVersion;
        }

        /// <summary>
        /// 更新分析参数
        /// </summary>
        public virtual void UpdateParameters(Dictionary<string, object> parameters)
        {
            Parameters = parameters ?? new Dictionary<string, object>();
        }

        /// <summary>
        /// 提交人工审核
        /// </summary>
        public virtual void SubmitForReview(string reason = null)
        {
            RequiresHumanReview = true;
            ReviewResult.Status = ReviewStatus.Pending;
            ReviewResult.SubmissionTime = DateTime.Now;
            ReviewResult.ReviewReason = reason;
        }

        /// <summary>
        /// 完成人工审核
        /// </summary>
        public virtual void CompleteReview(ReviewStatus status, Guid reviewerId, string reviewerName, string comments = null)
        {
            ReviewResult.Status = status;
            ReviewResult.ReviewerId = reviewerId;
            ReviewResult.ReviewerName = reviewerName;
            ReviewResult.Comments = comments;
            ReviewResult.ReviewTime = DateTime.Now;

            if (status == ReviewStatus.Approved)
            {
                RequiresHumanReview = false;
            }
        }

        /// <summary>
        /// 更新配置
        /// </summary>
        public virtual void UpdateConfiguration(AnalysisConfiguration configuration)
        {
            Configuration = configuration ?? new AnalysisConfiguration();
        }

        /// <summary>
        /// 检查是否需要人工审核
        /// </summary>
        private bool ShouldRequireHumanReview()
        {
            // 低置信度需要审核
            if (Confidence < Configuration.MinConfidenceForAutoApproval)
                return true;

            // 特定分析类型需要审核
            if (Configuration.RequireHumanReviewTypes.Contains(AnalysisType))
                return true;

            // 结果包含敏感内容需要审核
            if (Result.ContainsSensitiveContent())
                return true;

            return false;
        }

        /// <summary>
        /// 检查是否可以重试
        /// </summary>
        public virtual bool CanRetry()
        {
            return RetryCount < Configuration.MaxRetryCount &&
                   (Status == AnalysisStatus.Failed || Status == AnalysisStatus.Timeout);
        }

        /// <summary>
        /// 检查是否已完成
        /// </summary>
        public virtual bool IsCompleted()
        {
            return Status == AnalysisStatus.Completed ||
                   Status == AnalysisStatus.Failed ||
                   Status == AnalysisStatus.Cancelled;
        }
    }

    /// <summary>
    /// 分析类型枚举
    /// </summary>
    public enum AnalysisType
    {
        /// <summary>
        /// 人脸检测
        /// </summary>
        FaceDetection = 0,

        /// <summary>
        /// 人脸识别
        /// </summary>
        FaceRecognition = 1,

        /// <summary>
        /// 场景识别
        /// </summary>
        SceneRecognition = 2,

        /// <summary>
        /// 物体检测
        /// </summary>
        ObjectDetection = 3,

        /// <summary>
        /// 文字识别（OCR）
        /// </summary>
        TextRecognition = 4,

        /// <summary>
        /// 图像分类
        /// </summary>
        ImageClassification = 5,

        /// <summary>
        /// 情感分析
        /// </summary>
        EmotionAnalysis = 6,

        /// <summary>
        /// 美学评分
        /// </summary>
        AestheticScoring = 7,

        /// <summary>
        /// 质量评估
        /// </summary>
        QualityAssessment = 8,

        /// <summary>
        /// 颜色分析
        /// </summary>
        ColorAnalysis = 9,

        /// <summary>
        /// 风格识别
        /// </summary>
        StyleRecognition = 10,

        /// <summary>
        /// 年龄估计
        /// </summary>
        AgeEstimation = 11,

        /// <summary>
        /// 性别识别
        /// </summary>
        GenderRecognition = 12,

        /// <summary>
        /// 深度估计
        /// </summary>
        DepthEstimation = 13,

        /// <summary>
        /// 图像分割
        /// </summary>
        ImageSegmentation = 14,

        /// <summary>
        /// 相似度分析
        /// </summary>
        SimilarityAnalysis = 15,

        /// <summary>
        /// 异常检测
        /// </summary>
        AnomalyDetection = 16,

        /// <summary>
        /// 内容安全检测
        /// </summary>
        ContentSafety = 17,

        /// <summary>
        /// 地理位置识别
        /// </summary>
        LocationRecognition = 18,

        /// <summary>
        /// 时间估计
        /// </summary>
        TimeEstimation = 19,

        /// <summary>
        /// 天气识别
        /// </summary>
        WeatherRecognition = 20,

        /// <summary>
        /// 植物识别
        /// </summary>
        PlantRecognition = 21,

        /// <summary>
        /// 动物识别
        /// </summary>
        AnimalRecognition = 22,

        /// <summary>
        /// 建筑识别
        /// </summary>
        ArchitectureRecognition = 23,

        /// <summary>
        /// 食物识别
        /// </summary>
        FoodRecognition = 24,

        /// <summary>
        /// 艺术品识别
        /// </summary>
        ArtworkRecognition = 25,

        /// <summary>
        /// 标志识别
        /// </summary>
        LogoRecognition = 26,

        /// <summary>
        /// 车辆识别
        /// </summary>
        VehicleRecognition = 27,

        /// <summary>
        /// 服装识别
        /// </summary>
        ClothingRecognition = 28,

        /// <summary>
        /// 综合分析
        /// </summary>
        ComprehensiveAnalysis = 29
    }

    /// <summary>
    /// 分析状态枚举
    /// </summary>
    public enum AnalysisStatus
    {
        /// <summary>
        /// 等待中
        /// </summary>
        Pending = 0,

        /// <summary>
        /// 运行中
        /// </summary>
        Running = 1,

        /// <summary>
        /// 已完成
        /// </summary>
        Completed = 2,

        /// <summary>
        /// 失败
        /// </summary>
        Failed = 3,

        /// <summary>
        /// 已取消
        /// </summary>
        Cancelled = 4,

        /// <summary>
        /// 重试中
        /// </summary>
        Retrying = 5,

        /// <summary>
        /// 超时
        /// </summary>
        Timeout = 6,

        /// <summary>
        /// 等待审核
        /// </summary>
        PendingReview = 7
    }

    /// <summary>
    /// 分析优先级枚举
    /// </summary>
    public enum AnalysisPriority
    {
        /// <summary>
        /// 低优先级
        /// </summary>
        Low = 0,

        /// <summary>
        /// 普通优先级
        /// </summary>
        Normal = 1,

        /// <summary>
        /// 高优先级
        /// </summary>
        High = 2,

        /// <summary>
        /// 紧急优先级
        /// </summary>
        Urgent = 3,

        /// <summary>
        /// 实时优先级
        /// </summary>
        RealTime = 4
    }

    /// <summary>
    /// AI分析结果
    /// </summary>
    public class AIAnalysisResult
    {
        /// <summary>
        /// 检测对象列表
        /// </summary>
        public List<DetectedObject> DetectedObjects { get; set; } = new();

        /// <summary>
        /// 分类结果
        /// </summary>
        public List<ClassificationResult> Classifications { get; set; } = new();

        /// <summary>
        /// 文字识别结果
        /// </summary>
        public List<TextRegion> TextRegions { get; set; } = new();

        /// <summary>
        /// 颜色分析结果
        /// </summary>
        public ColorAnalysisResult ColorAnalysis { get; set; } = new();

        /// <summary>
        /// 质量评估结果
        /// </summary>
        public QualityAssessmentResult QualityAssessment { get; set; } = new();

        /// <summary>
        /// 美学评分结果
        /// </summary>
        public AestheticScoreResult AestheticScore { get; set; } = new();

        /// <summary>
        /// 情感分析结果
        /// </summary>
        public EmotionAnalysisResult EmotionAnalysis { get; set; } = new();

        /// <summary>
        /// 场景识别结果
        /// </summary>
        public SceneAnalysisResult SceneAnalysis { get; set; } = new();

        /// <summary>
        /// 风格分析结果
        /// </summary>
        public StyleAnalysisResult StyleAnalysis { get; set; } = new();

        /// <summary>
        /// 内容安全检测结果
        /// </summary>
        public ContentSafetyResult ContentSafety { get; set; } = new();

        /// <summary>
        /// 地理位置识别结果
        /// </summary>
        public LocationAnalysisResult LocationAnalysis { get; set; } = new();

        /// <summary>
        /// 时间估计结果
        /// </summary>
        public TimeAnalysisResult TimeAnalysis { get; set; } = new();

        /// <summary>
        /// 天气识别结果
        /// </summary>
        public WeatherAnalysisResult WeatherAnalysis { get; set; } = new();

        /// <summary>
        /// 相似度分析结果
        /// </summary>
        public SimilarityAnalysisResult SimilarityAnalysis { get; set; } = new();

        /// <summary>
        /// 原始响应数据
        /// </summary>
        public Dictionary<string, object> RawResponse { get; set; } = new();

        /// <summary>
        /// 分析摘要
        /// </summary>
        public AnalysisSummary Summary { get; set; } = new();

        /// <summary>
        /// 检查是否包含敏感内容
        /// </summary>
        public bool ContainsSensitiveContent()
        {
            return ContentSafety != null && ContentSafety.IsSensitive;
        }

        /// <summary>
        /// 获取主要标签
        /// </summary>
        public List<string> GetPrimaryTags(int maxCount = 10)
        {
            var tags = new List<string>();

            // 从分类结果中添加标签
            if (Classifications != null)
            {
                tags.AddRange(Classifications
                    .Where(c => c.Confidence > 0.5)
                    .OrderByDescending(c => c.Confidence)
                    .Take(maxCount / 2)
                    .Select(c => c.Label));
            }

            // 从检测对象中添加标签
            if (DetectedObjects != null)
            {
                tags.AddRange(DetectedObjects
                    .Where(d => d.Confidence > 0.5)
                    .OrderByDescending(d => d.Confidence)
                    .Take(maxCount / 2)
                    .Select(d => d.Label));
            }

            return tags.Take(maxCount).ToList();
        }
    }

    /// <summary>
    /// 检测对象
    /// </summary>
    public class DetectedObject
    {
        /// <summary>
        /// 对象ID
        /// </summary>
        public Guid Id { get; set; } = Guid.NewGuid();

        /// <summary>
        /// 对象标签
        /// </summary>
        public string Label { get; set; }

        /// <summary>
        /// 对象类别
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 边界框
        /// </summary>
        public BoundingBox BoundingBox { get; set; }

        /// <summary>
        /// 多边形轮廓
        /// </summary>
        public List<Point> Polygon { get; set; } = new();

        /// <summary>
        /// 对象属性
        /// </summary>
        public Dictionary<string, object> Attributes { get; set; } = new();

        /// <summary>
        /// 子对象
        /// </summary>
        public List<DetectedObject> SubObjects { get; set; } = new();

        /// <summary>
        /// 面积
        /// </summary>
        public double Area => BoundingBox?.Area ?? 0;

        /// <summary>
        /// 中心点
        /// </summary>
        public Point Center => BoundingBox != null
            ? new Point { X = BoundingBox.CenterX, Y = BoundingBox.CenterY }
            : new Point();
    }

    /// <summary>
    /// 点坐标
    /// </summary>
    public class Point
    {
        /// <summary>
        /// X坐标
        /// </summary>
        public double X { get; set; }

        /// <summary>
        /// Y坐标
        /// </summary>
        public double Y { get; set; }

        /// <summary>
        /// 计算到另一点的距离
        /// </summary>
        public double DistanceTo(Point other)
        {
            if (other == null) return double.MaxValue;
            double dx = X - other.X;
            double dy = Y - other.Y;
            return Math.Sqrt(dx * dx + dy * dy);
        }
    }

    /// <summary>
    /// 分类结果
    /// </summary>
    public class ClassificationResult
    {
        /// <summary>
        /// 标签
        /// </summary>
        public string Label { get; set; }

        /// <summary>
        /// 类别
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 排名
        /// </summary>
        public int Rank { get; set; }

        /// <summary>
        /// 同义词
        /// </summary>
        public List<string> Synonyms { get; set; } = new();

        /// <summary>
        /// 相关标签
        /// </summary>
        public List<string> RelatedTags { get; set; } = new();
    }

    /// <summary>
    /// 文字区域
    /// </summary>
    public class TextRegion
    {
        /// <summary>
        /// 区域ID
        /// </summary>
        public Guid Id { get; set; } = Guid.NewGuid();

        /// <summary>
        /// 识别的文字
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 边界框
        /// </summary>
        public BoundingBox BoundingBox { get; set; }

        /// <summary>
        /// 语言
        /// </summary>
        public string Language { get; set; }

        /// <summary>
        /// 文字类型（印刷体、手写等）
        /// </summary>
        public string TextType { get; set; }

        /// <summary>
        /// 文字方向
        /// </summary>
        public string Orientation { get; set; }

        /// <summary>
        /// 字体信息
        /// </summary>
        public Dictionary<string, object> FontInfo { get; set; } = new();
    }

    /// <summary>
    /// 颜色分析结果
    /// </summary>
    public class ColorAnalysisResult
    {
        /// <summary>
        /// 主要颜色
        /// </summary>
        public List<DominantColor> DominantColors { get; set; } = new();

        /// <summary>
        /// 色调分布
        /// </summary>
        public Dictionary<string, double> HueDistribution { get; set; } = new();

        /// <summary>
        /// 饱和度分布
        /// </summary>
        public Dictionary<string, double> SaturationDistribution { get; set; } = new();

        /// <summary>
        /// 亮度分布
        /// </summary>
        public Dictionary<string, double> BrightnessDistribution { get; set; } = new();

        /// <summary>
        /// 色彩和谐度评分
        /// </summary>
        public double ColorHarmonyScore { get; set; }

        /// <summary>
        /// 色彩对比度评分
        /// </summary>
        public double ColorContrastScore { get; set; }

        /// <summary>
        /// 主要色调
        /// </summary>
        public string DominantTone { get; set; }
    }

    /// <summary>
    /// 主要颜色
    /// </summary>
    public class DominantColor
    {
        /// <summary>
        /// 颜色名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 十六进制颜色值
        /// </summary>
        public string HexValue { get; set; }

        /// <summary>
        /// RGB值
        /// </summary>
        public RGBColor RGB { get; set; }

        /// <summary>
        /// HSV值
        /// </summary>
        public HSVColor HSV { get; set; }

        /// <summary>
        /// 占比
        /// </summary>
        public double Percentage { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }
    }

    /// <summary>
    /// RGB颜色
    /// </summary>
    public class RGBColor
    {
        /// <summary>
        /// 红色分量
        /// </summary>
        public int R { get; set; }

        /// <summary>
        /// 绿色分量
        /// </summary>
        public int G { get; set; }

        /// <summary>
        /// 蓝色分量
        /// </summary>
        public int B { get; set; }
    }

    /// <summary>
    /// HSV颜色
    /// </summary>
    public class HSVColor
    {
        /// <summary>
        /// 色调（0-360）
        /// </summary>
        public double H { get; set; }

        /// <summary>
        /// 饱和度（0-100）
        /// </summary>
        public double S { get; set; }

        /// <summary>
        /// 明度（0-100）
        /// </summary>
        public double V { get; set; }
    }

    /// <summary>
    /// 质量评估结果
    /// </summary>
    public class QualityAssessmentResult
    {
        /// <summary>
        /// 整体质量评分（0-100）
        /// </summary>
        public double OverallScore { get; set; }

        /// <summary>
        /// 清晰度评分
        /// </summary>
        public double SharpnessScore { get; set; }

        /// <summary>
        /// 噪点评分
        /// </summary>
        public double NoiseScore { get; set; }

        /// <summary>
        /// 曝光评分
        /// </summary>
        public double ExposureScore { get; set; }

        /// <summary>
        /// 对焦评分
        /// </summary>
        public double FocusScore { get; set; }

        /// <summary>
        /// 色彩评分
        /// </summary>
        public double ColorScore { get; set; }

        /// <summary>
        /// 构图评分
        /// </summary>
        public double CompositionScore { get; set; }

        /// <summary>
        /// 质量等级
        /// </summary>
        public QualityGrade QualityGrade { get; set; }

        /// <summary>
        /// 质量问题列表
        /// </summary>
        public List<QualityIssue> Issues { get; set; } = new();
    }

    /// <summary>
    /// 质量等级枚举
    /// </summary>
    public enum QualityGrade
    {
        /// <summary>
        /// 优秀
        /// </summary>
        Excellent = 0,

        /// <summary>
        /// 良好
        /// </summary>
        Good = 1,

        /// <summary>
        /// 一般
        /// </summary>
        Fair = 2,

        /// <summary>
        /// 较差
        /// </summary>
        Poor = 3,

        /// <summary>
        /// 很差
        /// </summary>
        VeryPoor = 4
    }

    /// <summary>
    /// 质量问题
    /// </summary>
    public class QualityIssue
    {
        /// <summary>
        /// 问题类型
        /// </summary>
        public string IssueType { get; set; }

        /// <summary>
        /// 问题描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 严重程度
        /// </summary>
        public Severity Severity { get; set; }

        /// <summary>
        /// 位置信息
        /// </summary>
        public BoundingBox Location { get; set; }

        /// <summary>
        /// 建议改进方案
        /// </summary>
        public string Suggestion { get; set; }
    }

    /// <summary>
    /// 严重程度枚举
    /// </summary>
    public enum Severity
    {
        /// <summary>
        /// 低
        /// </summary>
        Low = 0,

        /// <summary>
        /// 中
        /// </summary>
        Medium = 1,

        /// <summary>
        /// 高
        /// </summary>
        High = 2,

        /// <summary>
        /// 严重
        /// </summary>
        Critical = 3
    }

    /// <summary>
    /// 美学评分结果
    /// </summary>
    public class AestheticScoreResult
    {
        /// <summary>
        /// 整体美学评分（0-100）
        /// </summary>
        public double OverallScore { get; set; }

        /// <summary>
        /// 构图评分
        /// </summary>
        public double CompositionScore { get; set; }

        /// <summary>
        /// 色彩和谐度评分
        /// </summary>
        public double ColorHarmonyScore { get; set; }

        /// <summary>
        /// 视觉吸引力评分
        /// </summary>
        public double VisualAppealScore { get; set; }

        /// <summary>
        /// 情感冲击力评分
        /// </summary>
        public double EmotionalImpactScore { get; set; }

        /// <summary>
        /// 创意性评分
        /// </summary>
        public double CreativityScore { get; set; }

        /// <summary>
        /// 美学等级
        /// </summary>
        public AestheticGrade AestheticGrade { get; set; }

        /// <summary>
        /// 美学特征列表
        /// </summary>
        public List<AestheticFeature> Features { get; set; } = new();
    }

    /// <summary>
    /// 美学等级枚举
    /// </summary>
    public enum AestheticGrade
    {
        /// <summary>
        /// 杰作级
        /// </summary>
        Masterpiece = 0,

        /// <summary>
        /// 优秀级
        /// </summary>
        Excellent = 1,

        /// <summary>
        /// 良好级
        /// </summary>
        Good = 2,

        /// <summary>
        /// 一般级
        /// </summary>
        Average = 3,

        /// <summary>
        /// 待改进
        /// </summary>
        NeedsImprovement = 4
    }

    /// <summary>
    /// 美学特征
    /// </summary>
    public class AestheticFeature
    {
        /// <summary>
        /// 特征名称
        /// </summary>
        public string FeatureName { get; set; }

        /// <summary>
        /// 特征描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 评分
        /// </summary>
        public double Score { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }
    }

    /// <summary>
    /// 情感分析结果
    /// </summary>
    public class EmotionAnalysisResult
    {
        /// <summary>
        /// 主要情感
        /// </summary>
        public string PrimaryEmotion { get; set; }

        /// <summary>
        /// 情感分布
        /// </summary>
        public Dictionary<string, double> EmotionDistribution { get; set; } = new();

        /// <summary>
        /// 情感强度
        /// </summary>
        public double EmotionalIntensity { get; set; }

        /// <summary>
        /// 情感极性（正面/负面）
        /// </summary>
        public double SentimentPolarity { get; set; }

        /// <summary>
        /// 检测到的人脸情感
        /// </summary>
        public List<FaceEmotion> FaceEmotions { get; set; } = new();
    }

    /// <summary>
    /// 人脸情感
    /// </summary>
    public class FaceEmotion
    {
        /// <summary>
        /// 人脸边界框
        /// </summary>
        public BoundingBox FaceBoundingBox { get; set; }

        /// <summary>
        /// 主要情感
        /// </summary>
        public string Emotion { get; set; }

        /// <summary>
        /// 情感置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 所有情感的概率分布
        /// </summary>
        public Dictionary<string, double> AllEmotions { get; set; } = new();
    }

    /// <summary>
    /// 场景分析结果
    /// </summary>
    public class SceneAnalysisResult
    {
        /// <summary>
        /// 主要场景
        /// </summary>
        public string PrimaryScene { get; set; }

        /// <summary>
        /// 场景置信度
        /// </summary>
        public double SceneConfidence { get; set; }

        /// <summary>
        /// 场景类型
        /// </summary>
        public string SceneType { get; set; }

        /// <summary>
        /// 室内/室外
        /// </summary>
        public string IndoorOutdoor { get; set; }

        /// <summary>
        /// 天气条件
        /// </summary>
        public string WeatherCondition { get; set; }

        /// <summary>
        /// 时间段
        /// </summary>
        public string TimeOfDay { get; set; }

        /// <summary>
        /// 季节
        /// </summary>
        public string Season { get; set; }

        /// <summary>
        /// 所有场景候选
        /// </summary>
        public List<SceneCandidate> SceneCandidates { get; set; } = new();
    }

    /// <summary>
    /// 场景候选
    /// </summary>
    public class SceneCandidate
    {
        /// <summary>
        /// 场景名称
        /// </summary>
        public string SceneName { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 场景类别
        /// </summary>
        public string Category { get; set; }
    }

    /// <summary>
    /// 风格分析结果
    /// </summary>
    public class StyleAnalysisResult
    {
        /// <summary>
        /// 主要风格
        /// </summary>
        public string PrimaryStyle { get; set; }

        /// <summary>
        /// 风格置信度
        /// </summary>
        public double StyleConfidence { get; set; }

        /// <summary>
        /// 摄影风格
        /// </summary>
        public string PhotographyStyle { get; set; }

        /// <summary>
        /// 艺术风格
        /// </summary>
        public string ArtStyle { get; set; }

        /// <summary>
        /// 视觉风格
        /// </summary>
        public string VisualStyle { get; set; }

        /// <summary>
        /// 所有风格候选
        /// </summary>
        public List<StyleCandidate> StyleCandidates { get; set; } = new();
    }

    /// <summary>
    /// 风格候选
    /// </summary>
    public class StyleCandidate
    {
        /// <summary>
        /// 风格名称
        /// </summary>
        public string StyleName { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 风格类别
        /// </summary>
        public string Category { get; set; }
    }

    /// <summary>
    /// 内容安全检测结果
    /// </summary>
    public class ContentSafetyResult
    {
        /// <summary>
        /// 是否敏感
        /// </summary>
        public bool IsSensitive { get; set; }

        /// <summary>
        /// 敏感内容类型
        /// </summary>
        public List<string> SensitiveTypes { get; set; } = new();

        /// <summary>
        /// 风险等级
        /// </summary>
        public RiskLevel RiskLevel { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 检测到的敏感区域
        /// </summary>
        public List<SensitiveRegion> SensitiveRegions { get; set; } = new();

        /// <summary>
        /// 建议操作
        /// </summary>
        public List<string> RecommendedActions { get; set; } = new();
    }

    /// <summary>
    /// 风险等级枚举
    /// </summary>
    public enum RiskLevel
    {
        /// <summary>
        /// 安全
        /// </summary>
        Safe = 0,

        /// <summary>
        /// 低风险
        /// </summary>
        Low = 1,

        /// <summary>
        /// 中风险
        /// </summary>
        Medium = 2,

        /// <summary>
        /// 高风险
        /// </summary>
        High = 3,

        /// <summary>
        /// 极高风险
        /// </summary>
        Critical = 4
    }

    /// <summary>
    /// 敏感区域
    /// </summary>
    public class SensitiveRegion
    {
        /// <summary>
        /// 敏感类型
        /// </summary>
        public string SensitiveType { get; set; }

        /// <summary>
        /// 边界框
        /// </summary>
        public BoundingBox BoundingBox { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }
    }

    /// <summary>
    /// 地理位置分析结果
    /// </summary>
    public class LocationAnalysisResult
    {
        /// <summary>
        /// 识别地点
        /// </summary>
        public string IdentifiedLocation { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 国家
        /// </summary>
        public string Country { get; set; }

        /// <summary>
        /// 省份/州
        /// </summary>
        public string Province { get; set; }

        /// <summary>
        /// 城市
        /// </summary>
        public string City { get; set; }

        /// <summary>
        /// 具体地点
        /// </summary>
        public string SpecificPlace { get; set; }

        /// <summary>
        /// 地点类型
        /// </summary>
        public string PlaceType { get; set; }

        /// <summary>
        /// 所有候选地点
        /// </summary>
        public List<LocationCandidate> LocationCandidates { get; set; } = new();
    }

    /// <summary>
    /// 地点候选
    /// </summary>
    public class LocationCandidate
    {
        /// <summary>
        /// 地点名称
        /// </summary>
        public string PlaceName { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 地点详情
        /// </summary>
        public Dictionary<string, object> Details { get; set; } = new();
    }

    /// <summary>
    /// 时间分析结果
    /// </summary>
    public class TimeAnalysisResult
    {
        /// <summary>
        /// 估计时间
        /// </summary>
        public DateTime? EstimatedTime { get; set; }

        /// <summary>
        /// 时间置信度
        /// </summary>
        public double TimeConfidence { get; set; }

        /// <summary>
        /// 时间段
        /// </summary>
        public string TimeOfDay { get; set; }

        /// <summary>
        /// 季节
        /// </summary>
        public string Season { get; set; }

        /// <summary>
        /// 年代估计
        /// </summary>
        public string Decade { get; set; }

        /// <summary>
        /// 时间范围
        /// </summary>
        public string TimeRange { get; set; }
    }

    /// <summary>
    /// 天气分析结果
    /// </summary>
    public class WeatherAnalysisResult
    {
        /// <summary>
        /// 天气状况
        /// </summary>
        public string WeatherCondition { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 温度估计
        /// </summary>
        public TemperatureRange Temperature { get; set; }

        /// <summary>
        /// 所有天气候选
        /// </summary>
        public List<WeatherCandidate> WeatherCandidates { get; set; } = new();
    }

    /// <summary>
    /// 温度范围
    /// </summary>
    public class TemperatureRange
    {
        /// <summary>
        /// 最低温度
        /// </summary>
        public double? MinTemperature { get; set; }

        /// <summary>
        /// 最高温度
        /// </summary>
        public double? MaxTemperature { get; set; }

        /// <summary>
        /// 温度单位
        /// </summary>
        public string Unit { get; set; } = "Celsius";
    }

    /// <summary>
    /// 天气候选
    /// </summary>
    public class WeatherCandidate
    {
        /// <summary>
        /// 天气状况
        /// </summary>
        public string Weather { get; set; }

        /// <summary>
        /// 置信度
        /// </summary>
        public double Confidence { get; set; }
    }

    /// <summary>
    /// 相似度分析结果
    /// </summary>
    public class SimilarityAnalysisResult
    {
        /// <summary>
        /// 相似照片列表
        /// </summary>
        public List<SimilarPhoto> SimilarPhotos { get; set; } = new();

        /// <summary>
        /// 重复照片列表
        /// </summary>
        public List<DuplicatePhoto> DuplicatePhotos { get; set; } = new();

        /// <summary>
        /// 最相似照片
        /// </summary>
        public SimilarPhoto MostSimilar { get; set; }

        /// <summary>
        /// 整体相似度分布
        /// </summary>
        public Dictionary<string, int> SimilarityDistribution { get; set; } = new();
    }

    /// <summary>
    /// 相似照片
    /// </summary>
    public class SimilarPhoto
    {
        /// <summary>
        /// 照片ID
        /// </summary>
        public Guid PhotoId { get; set; }

        /// <summary>
        /// 相似度分数（0-1）
        /// </summary>
        public double SimilarityScore { get; set; }

        /// <summary>
        /// 相似类型
        /// </summary>
        public SimilarityType SimilarityType { get; set; }

        /// <summary>
        /// 匹配区域
        /// </summary>
        public List<MatchingRegion> MatchingRegions { get; set; } = new();
    }

    /// <summary>
    /// 相似类型枚举
    /// </summary>
    public enum SimilarityType
    {
        /// <summary>
        /// 完全相同
        /// </summary>
        Identical = 0,

        /// <summary>
        /// 近似重复
        /// </summary>
        NearDuplicate = 1,

        /// <summary>
        /// 相似内容
        /// </summary>
        SimilarContent = 2,

        /// <summary>
        /// 相似场景
        /// </summary>
        SimilarScene = 3,

        /// <summary>
        /// 相似构图
        /// </summary>
        SimilarComposition = 4,

        /// <summary>
        /// 相似色彩
        /// </summary>
        SimilarColors = 5,

        /// <summary>
        /// 相似风格
        /// </summary>
        SimilarStyle = 6
    }

    /// <summary>
    /// 匹配区域
    /// </summary>
    public class MatchingRegion
    {
        /// <summary>
        /// 源图像边界框
        /// </summary>
        public BoundingBox SourceBox { get; set; }

        /// <summary>
        /// 目标图像边界框
        /// </summary>
        public BoundingBox TargetBox { get; set; }

        /// <summary>
        /// 区域相似度
        /// </summary>
        public double RegionSimilarity { get; set; }
    }

    /// <summary>
    /// 重复照片
    /// </summary>
    public class DuplicatePhoto
    {
        /// <summary>
        /// 照片ID
        /// </summary>
        public Guid PhotoId { get; set; }

        /// <summary>
        /// 重复类型
        /// </summary>
        public DuplicateType DuplicateType { get; set; }

        /// <summary>
        /// 质量比较（1表示当前照片更好）
        /// </summary>
        public double QualityComparison { get; set; }

        /// <summary>
        /// 推荐保留哪张
        /// </summary>
        public Guid RecommendedKeepId { get; set; }
    }

    /// <summary>
    /// 重复类型枚举
    /// </summary>
    public enum DuplicateType
    {
        /// <summary>
        /// 完全相同
        /// </summary>
        Exact = 0,

        /// <summary>
        /// 近似相同
        /// </summary>
        NearExact = 1,

        /// <summary>
        /// 编辑版本
        /// </summary>
        EditedVersion = 2,

        /// <summary>
        /// 不同分辨率
        /// </summary>
        DifferentResolution = 3,

        /// <summary>
        /// 不同格式
        /// </summary>
        DifferentFormat = 4
    }

    /// <summary>
    /// 分析摘要
    /// </summary>
    public class AnalysisSummary
    {
        /// <summary>
        /// 主要标签
        /// </summary>
        public List<string> PrimaryTags { get; set; } = new();

        /// <summary>
        /// 关键对象
        /// </summary>
        public List<string> KeyObjects { get; set; } = new();

        /// <summary>
        /// 场景描述
        /// </summary>
        public string SceneDescription { get; set; }

        /// <summary>
        /// 内容描述
        /// </summary>
        public string ContentDescription { get; set; }

        /// <summary>
        /// 整体评分
        /// </summary>
        public double OverallScore { get; set; }

        /// <summary>
        /// 推荐操作
        /// </summary>
        public List<string> RecommendedActions { get; set; } = new();

        /// <summary>
        /// 分析时间戳
        /// </summary>
        public DateTime AnalysisTimestamp { get; set; }
    }

    /// <summary>
    /// 人工审核结果
    /// </summary>
    public class HumanReviewResult
    {
        /// <summary>
        /// 审核状态
        /// </summary>
        public ReviewStatus Status { get; set; }

        /// <summary>
        /// 审核者ID
        /// </summary>
        public Guid? ReviewerId { get; set; }

        /// <summary>
        /// 审核者名称
        /// </summary>
        public string ReviewerName { get; set; }

        /// <summary>
        /// 提交时间
        /// </summary>
        public DateTime? SubmissionTime { get; set; }

        /// <summary>
        /// 审核时间
        /// </summary>
        public DateTime? ReviewTime { get; set; }

        /// <summary>
        /// 审核原因
        /// </summary>
        public string ReviewReason { get; set; }

        /// <summary>
        /// 审核意见
        /// </summary>
        public string Comments { get; set; }

        /// <summary>
        /// 审核耗时（分钟）
        /// </summary>
        public double? ReviewDurationMinutes { get; set; }

        /// <summary>
        /// 修正结果
        /// </summary>
        public Dictionary<string, object> Corrections { get; set; } = new();
    }

    /// <summary>
    /// 审核状态枚举
    /// </summary>
    public enum ReviewStatus
    {
        /// <summary>
        /// 等待审核
        /// </summary>
        Pending = 0,

        /// <summary>
        /// 审核中
        /// </summary>
        InReview = 1,

        /// <summary>
        /// 已批准
        /// </summary>
        Approved = 2,

        /// <summary>
        /// 已拒绝
        /// </summary>
        Rejected = 3,

        /// <summary>
        /// 需要修改
        /// </summary>
        NeedsRevision = 4
    }

    /// <summary>
    /// 分析配置
    /// </summary>
    public class AnalysisConfiguration
    {
        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetryCount { get; set; } = 3;

        /// <summary>
        /// 超时时间（分钟）
        /// </summary>
        public int TimeoutMinutes { get; set; } = 30;

        /// <summary>
        /// 自动批准的最低置信度
        /// </summary>
        public double MinConfidenceForAutoApproval { get; set; } = 0.8;

        /// <summary>
        /// 需要人工审核的分析类型
        /// </summary>
        public List<AnalysisType> RequireHumanReviewTypes { get; set; } = new();

        /// <summary>
        /// 是否启用缓存
        /// </summary>
        public bool EnableCache { get; set; } = true;

        /// <summary>
        /// 缓存有效期（小时）
        /// </summary>
        public int CacheExpiryHours { get; set; } = 24;

        /// <summary>
        /// 是否启用并行处理
        /// </summary>
        public bool EnableParallelProcessing { get; set; } = false;

        /// <summary>
        /// 并行处理数量
        /// </summary>
        public int ParallelProcessingCount { get; set; } = 1;

        /// <summary>
        /// 资源限制
        /// </summary>
        public AnalysisResourceLimits ResourceLimits { get; set; } = new();

        /// <summary>
        /// 输出配置
        /// </summary>
        public AnalysisOutputConfig OutputConfig { get; set; } = new();
    }

    /// <summary>
    /// 分析资源限制
    /// </summary>
    public class AnalysisResourceLimits
    {
        /// <summary>
        /// 最大内存使用量（MB）
        /// </summary>
        public long MaxMemoryMB { get; set; } = 2048;

        /// <summary>
        /// 最大CPU使用率（百分比）
        /// </summary>
        public double MaxCpuUsage { get; set; } = 80;

        /// <summary>
        /// 最大GPU使用率（百分比）
        /// </summary>
        public double MaxGpuUsage { get; set; } = 90;

        /// <summary>
        /// 最大处理时间（分钟）
        /// </summary>
        public int MaxProcessingTimeMinutes { get; set; } = 60;
    }

    /// <summary>
    /// 分析输出配置
    /// </summary>
    public class AnalysisOutputConfig
    {
        /// <summary>
        /// 输出格式
        /// </summary>
        public List<string> OutputFormats { get; set; } = new() { "JSON" };

        /// <summary>
        /// 是否保存原始响应
        /// </summary>
        public bool SaveRawResponse { get; set; } = true;

        /// <summary>
        /// 是否保存可视化结果
        /// </summary>
        public bool SaveVisualizationResults { get; set; } = false;

        /// <summary>
        /// 是否保存中间结果
        /// </summary>
        public bool SaveIntermediateResults { get; set; } = false;

        /// <summary>
        /// 输出压缩级别
        /// </summary>
        public int OutputCompressionLevel { get; set; } = 6;
    }

    /// <summary>
    /// AI性能指标
    /// </summary>
    public class AIPerformanceMetrics
    {
        /// <summary>
        /// 执行耗时（毫秒）
        /// </summary>
        public long ExecutionDuration { get; set; }

        /// <summary>
        /// 内存使用量（字节）
        /// </summary>
        public long MemoryUsage { get; set; }

        /// <summary>
        /// GPU内存使用量（字节）
        /// </summary>
        public long GpuMemoryUsage { get; set; }

        /// <summary>
        /// CPU使用率
        /// </summary>
        public double CpuUsage { get; set; }

        /// <summary>
        /// GPU使用率
        /// </summary>
        public double GpuUsage { get; set; }

        /// <summary>
        /// 网络传输量（字节）
        /// </summary>
        public long NetworkTransferBytes { get; set; }

        /// <summary>
        /// 模型加载时间（毫秒）
        /// </summary>
        public long ModelLoadTimeMs { get; set; }

        /// <summary>
        /// 预处理时间（毫秒）
        /// </summary>
        public long PreprocessingTimeMs { get; set; }

        /// <summary>
        /// 推理时间（毫秒）
        /// </summary>
        public long InferenceTimeMs { get; set; }

        /// <summary>
        /// 后处理时间（毫秒）
        /// </summary>
        public long PostprocessingTimeMs { get; set; }

        /// <summary>
        /// 缓存命中率
        /// </summary>
        public double CacheHitRate { get; set; }

        /// <summary>
        /// 吞吐量（每秒处理数量）
        /// </summary>
        public double ThroughputPerSecond { get; set; }

        /// <summary>
        /// 更新性能指标
        /// </summary>
        public void UpdateMetrics(AIPhotoAnalysis analysis)
        {
            if (analysis.ExecutionDuration > 0)
            {
                ThroughputPerSecond = 1000.0 / analysis.ExecutionDuration;
            }
        }
    }
}